<template>
  <div class="app-container">
    <div class="app-main">
      <div class="mainName">会员单位人员信息采集</div>
      <div class="btnGroup">
        <el-button type="primary" size="small" @click="addPerson">录入人员</el-button>
        <el-button
          type="primary"
          size="small"
          @click="editPerson"
          v-show="currentRow.applyState === '0'"
        >编辑</el-button>
        <el-button
          type="primary"
          size="small"
          @click="addPerson"
          v-show="currentRow.applyState === '0'"
        >提交审核</el-button>
        <el-button
          type="danger"
          size="small"
          @click="addPerson"
          v-show="currentRow.applyState === '0'"
        >删除</el-button>
        <el-button
          type="danger"
          size="small"
          @click="review"
          v-show="currentRow.applyState == '1'"
        >选中撤销提交审核</el-button>
      </div>
      <el-table
        ref="reference"
        v-loading="loading"
        :data="list"
        border
        stripe
        highlight-current-row
        @current-change="handleCurrentChange"
        :key="1"
      >
        <el-table-column label="序号" align="center" type="index"></el-table-column>
        <el-table-column label="申请编号" align="center" prop="applyCode"/>
        <el-table-column label="单位名称" align="center" prop="controlName" min-width="220"/>
        <el-table-column label="创建日期" align="center" prop="createTime"/>
        <el-table-column label="操作员" align="center" prop="operator"/>
        <el-table-column label="状态" align="center" prop="applyState">
          <template slot-scope="scope">
            <div v-if="scope.row.applyState === '1'">待审核</div>
            <div v-if="scope.row.applyState === '0'">编辑</div>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        class="pagination-container"
      />
    </div>
    <el-dialog title="会员单位人员信息采集→新增人员信息" :visible.sync="dialogVisible" width="80%">
      <div class="operate">
        <el-button type="primary" size="small" @click="addItem">新增</el-button>
        <el-button
          type="danger"
          size="small"
          @click="deleteItem"
          :disabled="multipleSelection.length <= 0"
        >删除选中</el-button>
      </div>
      <el-table
        ref="personList"
        :data="personList"
        border
        stripe
        :key="2"
        @selection-change="handleSelectionChange"
        style="margin-top: 8px;"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" align="center" type="index"></el-table-column>
        <el-table-column label="人员姓名" align="center" prop="operatorName">
          <template slot-scope="scope">
            <span class="red">*</span>
            <div class="inputWidth">
              <el-input v-model="scope.row.operatorName" placeholder="请输入" size="mini"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="人员性质" align="center" prop="operatorNature">
          <template slot-scope="scope">
            <span class="red">*</span>
            <div class="inputWidth">
              <el-select v-model="scope.row.operatorNature" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in natureList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center" prop="gender">
          <template slot-scope="scope">
            <span class="red">*</span>
            <div class="inputWidth">
              <el-select v-model="scope.row.gender" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in genderList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="年龄" align="center" prop="age">
          <template slot-scope="scope">
            <span class="red">*</span>
            <div class="inputWidth">
              <el-input v-model="scope.row.age" placeholder="请输入" size="mini"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="最高学历" align="center" prop="educational">
          <template slot-scope="scope">
            <span class="red">*</span>
            <div class="inputWidth">
              <el-select v-model="scope.row.educational" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in educationalList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="持证名称" align="center" prop="certificate">
          <template slot-scope="scope">
            <div class="inputWidth">
              <el-input v-model="scope.row.certificate" placeholder="请输入" size="mini"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="证件编号" align="center" prop="certificateCode">
          <template slot-scope="scope">
            <div class="inputWidth">
              <el-input v-model="scope.row.certificateCode" placeholder="请输入" size="mini"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="发行单位" align="center" prop="issueUnit">
          <template slot-scope="scope">
            <div class="inputWidth">
              <el-input v-model="scope.row.issueUnit" placeholder="请输入" size="mini"></el-input>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="mini">取 消</el-button>
        <el-button type="primary" @click="submit" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  companyApplyQuery, // 人员信息
  companyOperatorAdd, // 新增人员信息
  companyOperatorUpdate, // 新增人员信息-编辑
  companyOperatorQueryByApplyCode, // 根据申请编号查询本申请详细信息
  companyApplyCancel, // 选中撤销提交审核
} from "@/api/manage/approval";
export default {
  name: "archivesList",
  data() {
    return {
      // 遮罩层
      loading: false,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      personList: [
        {
          operatorName: "",
          operatorNature: "",
          gender: "",
          age: "",
          educational: "",
          certificate: "",
          certificateCode: "",
          issueUnit: ""
        }
      ],
      natureList: [
        {
          value: "管理人员",
          label: "管理人员"
        },
        {
          value: "技术人员",
          label: "技术人员"
        },
        {
          value: "其他人员",
          label: "其他人员"
        }
      ],
      genderList: [
        {
          value: "1",
          label: "男"
        },
        {
          value: "2",
          label: "女"
        },
        {
          value: "3",
          label: "其他"
        }
      ],
      educationalList: [
        {
          value: "研究生",
          label: "研究生"
        },
        {
          value: "本科",
          label: "本科"
        },
        {
          value: "大专",
          label: "大专"
        },
        {
          value: "高中及中专",
          label: "高中及中专"
        },
        {
          value: "高中以下",
          label: "高中以下"
        }
      ],
      currentRow: null,
      applyId: "",
      multipleSelection: []
    };
  },
  mounted() {
    this.getList();
  },
  watch: {
    currentRow: {
      handler(newVal, oldVal) {
        console.log(newVal, oldVal, "监听");
      },
      deep: true
    }
  },
  methods: {
    getList() {
      this.loading = true;
      const that = this;
      companyApplyQuery(this.queryParams)
        .then(res => {
          console.log(res, "23232");
          if (res.errorCode == "000000") {
            this.list = res.data.list;
            this.total = res.data.total;
            if (this.list.length > 0) {
              that.setCurrent(this.list[0]);
            }
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    editPerson() {
      this.applyId = this.currentRow.applyId;
      this.dialogVisible = true;
      const query = {
        applyCode: this.currentRow.applyCode
      };
      companyOperatorQueryByApplyCode(query).then(res => {
        if (res.errorCode === "000000") {
          console.log(res, "hsjhsjhjhj");
          const { response } = res.data;
          if (response) {
            this.personList = response;
          } else {
            this.personList = [];
          }
        }
      });
    },
    addPerson() {
      this.applyId = "";
      this.dialogVisible = true;
    },
    handleCurrentChange(val) {
      console.log(val, "-----");
      this.currentRow = val;
    },
    setCurrent(row) {
      this.currentRow = row;
      this.$nextTick(() => {
        this.$refs.reference.setCurrentRow(row);
      });
    },
    review() {
      console.log(this.currentRow, 'companyApplyCancel');
      const data = [this.currentRow.applyId]
      this.$modal
        .confirm("是否确认撤销提交审核？")
        .then(function() {
          return companyApplyCancel(data);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("撤销成功！");
        })
        .catch(() => {});
    },
    addItem() {
      if (this.personList?.length > 0) {
        let index = this.personList.length - 1;
        if (
          this.personList[index].operatorName === "" ||
          this.personList[index].operatorNature === "" ||
          this.personList[index].gender === "" ||
          this.personList[index].age === "" ||
          this.personList[index].educational === ""
        ) {
          this.$modal.msgWarning(`请先完善第${index + 1}条人员信息！`);
        } else {
          this.personList.push({ ...this.personList[index] });
        }
      } else {
        this.personList.push({
          operatorName: "",
          operatorNature: "",
          gender: "",
          age: "",
          educational: "",
          certificate: "",
          certificateCode: "",
          issueUnit: ""
        });
      }
    },
    submit() {
      //companyOperatorAdd

      if (this.personList?.length > 0) {
        let index = this.personList.length - 1;
        if (
          this.personList[index].operatorName === "" ||
          this.personList[index].operatorNature === "" ||
          this.personList[index].gender === "" ||
          this.personList[index].age === "" ||
          this.personList[index].educational === ""
        ) {
          this.$modal.msgWarning(`请先完善第${index + 1}条人员信息！`);
        } else {
          if (this.applyId) {
            // 编辑
            const data = {
              request: this.personList.map(item => {
                return {
                  ...item, 
                  applyCode: this.currentRow.applyCode
                }
              })
            };
            companyOperatorUpdate(data)
              .then(res => {
                console.log(res, "编辑");
                if (res.errorCode === "000000") {
                  this.$modal.msgSuccess("编辑成功！");
                  this.getList();
                }
              })
              .finally(() => {
                this.cancel();
              });
          } else {
            // 新增接口
            const data = {
              request: this.personList
            };
            companyOperatorAdd(data)
              .then(res => {
                console.log(res, "resssssssss");
                if (res.errorCode === "000000") {
                  this.$modal.msgSuccess("新增成功！");
                  this.getList();
                }
              })
              .finally(() => {
                this.cancel();
              });
          }
        }
      } else {
        this.$modal.msgWarning("至少添加一条人员信息！");
      }
    },
    cancel() {
      this.dialogVisible = false;
      this.personList = [
        {
          operatorName: "",
          operatorNature: "",
          gender: "",
          age: "",
          educational: "",
          certificate: "",
          certificateCode: "",
          issueUnit: ""
        }
      ];
    },
    handleSelectionChange(val) {
      console.log(val, "jkjkjk");
      this.multipleSelection = val;
    },
    deleteItem() {
      console.log(this.multipleSelection, "multipleSelection");
      console.log(this.personList, "personList");
      this.personList = this.personList.filter(
        row => !this.multipleSelection.includes(row)
      );
      console.log(this.personList, "personListaaa");
      this.$refs.personList.clearSelection();
      this.multipleSelection = [];
    }
  }
};
</script>
<style lang="scss" scoped>
.app-main {
  background-color: #fff;
  // padding: 15px 20px;
  border-radius: 8px;
  .mainName {
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 16px;
    height: 30px;
    line-height: 30px;
    color: #000000;
    border-bottom: 1px solid #dcdfe6;
    margin-bottom: 10px;
  }
  .btnGroup {
    text-align: right;
    margin: 15px 0;
  }
}
::v-deep .el-dialog__header {
  border-bottom: 1px solid #e9e9ec;
}
.operate {
  text-align: right;
}
.red {
  color: red;
  margin-right: 2px;
  vertical-align: top;
}
.inputWidth {
  display: inline-block;
  width: calc(100% - 8px);
}
::v-deep .el-table__body tr.current-row > td.el-table__cell {
  background-color: #fabe91 !important;
}
</style>